package com.xposed.xinhao.hook_microsupply;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.xposed.xinhao.utils.JsonUtils;
import com.xposed.xinhao.utils.LogUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * 采源宝V3.5.1
 * */
public class MicrosupplyHook {

    //保存上下文
    private Context mContext;

    private XC_LoadPackage.LoadPackageParam lpparam;

    public MicrosupplyHook(XC_LoadPackage.LoadPackageParam lpparam) {
        this.lpparam = lpparam;
    }

    public void hookMethod() {
        XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                mContext = (Context) param.thisObject;
                LogUtils.e("Activity onCreate：" + mContext.getClass().getName());
            }
        });


        Class<?> Event = XposedHelpers.findClassIfExists("com.alibaba.wireless.mvvm.event.Event",lpparam.classLoader);
        XposedHelpers.findAndHookConstructor(Event, View.class, String.class, String.class, int.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                LogUtils.e("Event：" + param.args[0] + ","+ param.args[1] + ","+ param.args[2] + ","+ param.args[3]);
            }
        });

        XposedHelpers.findAndHookMethod("com.alibaba.wireless.microsupply.business.order.OrderResultActivity", lpparam.classLoader,"onCreate", Bundle.class,new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        LogUtils.e("===================================");

                        Object object = XposedHelpers.getObjectField(param.thisObject,"orderModel");
                        Object pojo =  XposedHelpers.getObjectField(object,"pojo");
                        Object orderIds = XposedHelpers.getObjectField(pojo,"orderIds");
                        Object resultItems = XposedHelpers.getObjectField(pojo,"resultItems");
                        LogUtils.e("orderIds：" + orderIds);
                        LogUtils.e("resultItems：" + JsonUtils.objectToString(resultItems));
                    }
                }).start();

               // Object o = ((Activity)param.thisObject).getIntent().getParcelableExtra("result_data");

//                LogUtils.e("OrderResultActivity onCreate：" + JsonUtils.objectToString(param.getResult()));
            }
        });
    }
}
